﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using GmaWebService.Code.Helper;
using GmaWebService.Interfaces;

namespace GmaWebService.Code
{
    internal class MovingPictures : Database
    {
        public MovingPictures() :
            base(Utils.GetMPDbLocations().MovingPictures)
        {
        }

        #region Movies
        public WebMovieFull GetFullMovie(int idMovie)
        {
            string sql = "Select info.id, local.fullpath, local.discid, local.filehash, local.part, local.duration, local.videowidth, local.videoheight, local.videoresolution, local.videocodec, " +
                         "local.audiocodec, local.audiochannels, local.hassubtitles, local.videoformat, " +
                         "movie.title, movie.alternate_titles, movie.sortby, movie.directors, movie.writers, movie.actors, " +
                         "movie.year, movie.genres, movie.certification, movie.language, movie.tagline, movie.summary, movie.score, movie.popularity, movie.date_added, movie.runtime, movie.imdb_id, " +
                         "movie.alternatecovers, movie.coverfullpath, movie.coverthumbfullpath, movie.backdropfullpath " +
                         "from local_media as local, movie_info as movie, local_media__movie_info as info where local.id = info.local_media_id and movie.id = info.movie_info_id and info.id = " + idMovie;
            return ReadRow<WebMovieFull>(sql, FillFullMovie);
        }

        private WebMovieFull FillFullMovie(SQLiteDataReader reader)
        {
            try
            {
                WebMovieFull movie = new WebMovieFull();
                movie.Id = DatabaseHelperMethods.SafeInt32(reader, 0);
                movie.Filename = DatabaseHelperMethods.SafeStr(reader, 1);
                movie.DiscId = DatabaseHelperMethods.SafeStr(reader, 2);
                movie.Hash = DatabaseHelperMethods.SafeStr(reader, 3);
                movie.Part = DatabaseHelperMethods.SafeInt32(reader, 4);
                movie.Duration = DatabaseHelperMethods.SafeInt32(reader, 5);
                movie.VideoWidth = DatabaseHelperMethods.SafeInt32(reader, 6);
                movie.VideoHeight = DatabaseHelperMethods.SafeInt32(reader, 7);
                movie.VideoResolution = DatabaseHelperMethods.SafeStr(reader, 8);
                movie.VideoCodec = DatabaseHelperMethods.SafeStr(reader, 9);
                movie.AudioCodec = DatabaseHelperMethods.SafeStr(reader, 10);
                movie.AudioChannels = DatabaseHelperMethods.SafeStr(reader, 11);
                movie.HasSubtitles = DatabaseHelperMethods.SafeBoolean(reader, 12);
                movie.VideoFormat = DatabaseHelperMethods.SafeStr(reader, 13);

                movie.Title = DatabaseHelperMethods.SafeStr(reader, 14);
                movie.AlternateTitles = DatabaseHelperMethods.SafeStr(reader, 15);
                movie.SortBy = DatabaseHelperMethods.SafeStr(reader, 16);
                movie.Directors = DatabaseHelperMethods.SafeStr(reader, 17);
                movie.Writers = DatabaseHelperMethods.SafeStr(reader, 18);
                movie.Actors = DatabaseHelperMethods.SafeStr(reader, 19);
                movie.Year = DatabaseHelperMethods.SafeInt32(reader, 20);
                movie.Genre = DatabaseHelperMethods.SafeStr(reader, 21);
                movie.Certification = DatabaseHelperMethods.SafeStr(reader, 22);
                movie.Language = DatabaseHelperMethods.SafeStr(reader, 23);
                movie.TagLine = DatabaseHelperMethods.SafeStr(reader, 24);
                movie.Summary = DatabaseHelperMethods.SafeStr(reader, 25);
                movie.Score = DatabaseHelperMethods.SafeFloat(reader, 26);
                movie.Popularity = DatabaseHelperMethods.SafeInt32(reader, 27);
                movie.DateAdded = DatabaseHelperMethods.SafeDateTime(reader, 28);
                movie.Runtime = DatabaseHelperMethods.SafeInt32(reader, 29);
                movie.ImdbId = DatabaseHelperMethods.SafeStr(reader, 30);
                movie.CoverPathAlternate = DatabaseHelperMethods.SafeStr(reader, 31);
                movie.CoverPath = DatabaseHelperMethods.SafeStr(reader, 32);
                movie.CoverThumbPath = DatabaseHelperMethods.SafeStr(reader, 33);
                movie.BackdropPath = DatabaseHelperMethods.SafeStr(reader, 34);
                //movie.CoverPathAlternate = m_webServiceHelper.CreateUrl(DatabaseHelperMethods.SafeStr(reader, 31), BannerTypes.MovingPicturesPoster);
                //movie.CoverPath = m_webServiceHelper.CreateUrl(DatabaseHelperMethods.SafeStr(reader, 32), BannerTypes.MovingPicturesPoster);
                //movie.CoverThumbPath = m_webServiceHelper.CreateUrl(DatabaseHelperMethods.SafeStr(reader, 33), BannerTypes.MovingPicturesPoster);
                //movie.BackdropPath = m_webServiceHelper.CreateUrl(DatabaseHelperMethods.SafeStr(reader, 34), BannerTypes.MovingPicturesPoster);

                return movie;
            }
            catch (Exception ex)
            {
                Log.Error("Error reading movie details", ex);
                return null;
            }
        }

        public int GetMovieCount()
        {
            return ReadInt("Select COUNT(info.id) " +
                           "from local_media as local, movie_info as movie, local_media__movie_info as info " +
                           "where local.id = info.local_media_id and movie.id = info.movie_info_id");
        }

        public List<WebMovie> GetAllMovies()
        {
            return GetMovies(null, null); 
        }

        public List<WebMovie> GetMovies(int? start, int? end)
        {
            string sql = "Select info.id, local.fullpath, " +
                         "movie.title, movie.tagline,  " +
                         "movie.year, movie.genres, movie.coverthumbfullpath, movie.backdropfullpath " +
                         "from local_media as local, movie_info as movie, local_media__movie_info as info where local.id = info.local_media_id and movie.id = info.movie_info_id";
            return ReadList<WebMovie>(sql, FillBasicMovie, start, end);
        }

        private WebMovie FillBasicMovie(SQLiteDataReader reader)
        {
            try
            {
                WebMovie movie = new WebMovie();
                int id = DatabaseHelperMethods.SafeInt32(reader, 0);
                String path = DatabaseHelperMethods.SafeStr(reader, 1);
                String title = DatabaseHelperMethods.SafeStr(reader, 2);
                String tagline = DatabaseHelperMethods.SafeStr(reader, 3);
                int year = DatabaseHelperMethods.SafeInt32(reader, 4);
                String genres = DatabaseHelperMethods.SafeStr(reader, 5);
                String coverThumbPath = DatabaseHelperMethods.SafeStr(reader, 6);
                String backdropFullPath = DatabaseHelperMethods.SafeStr(reader, 7);
                //coverThumbPath = m_webServiceHelper.CreateUrl(coverThumbPath, BannerTypes.MovingPicturesPoster);

                movie.Id = id;
                movie.Filename = path;
                movie.TagLine = tagline;
                movie.Title = title;
                movie.Year = year;
                movie.Genre = genres;
                movie.CoverThumbPath = coverThumbPath;
                movie.BackdropPath = backdropFullPath;

                return movie;
            }
            catch (Exception ex)
            {
                Log.Error("Error reading movie information", ex);
                return null;
            }
        }

        public List<WebMovie> SearchForMovie(String searchString)
        {
            string sql = "Select info.id, local.fullpath, " +
                         "movie.title, movie.tagline,  " +
                         "movie.year, movie.genres, movie.coverthumbfullpath, movie.backdropfullpath " +
                         "from local_media as local, movie_info as movie, local_media__movie_info as info " +
                         "where local.id = info.local_media_id and movie.id = info.movie_info_id and " +
                         "movie.title LIKE '%" + searchString + "%'";
            return ReadList<WebMovie>(sql, FillBasicMovie);
        }
        #endregion

        /// <summary>
        /// Gets the path to a media item
        /// </summary>
        /// <param name="itemId">Id of the media item</param>
        /// <returns>Path to the mediaitem or null if item id doesn't exist</returns>
        internal string GetMoviePath(string itemId)
        {
            try
            {
                WebMovieFull movie = GetFullMovie(Int32.Parse(itemId));
                if (movie != null)
                {
                    return movie.Filename;
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error getting movie path for " + itemId, ex);
            }
            return null;
        }
    }
}